import 'package:drive/drive.dart';
import 'package:flutter/material.dart';

import 'view.dart';
import 'http.dart';
import 'package:flutter_mvc/flutter_mvc.dart';
import 'dart:convert';
import 'package:crypto/crypto.dart';
///Description:注册
///Author:teihideharu
class RegisterController extends BaseController {

   final textPhoneController=TextEditingController();
   final textPwdController=TextEditingController();
   final textNickController=TextEditingController();
   final textRePwdController=TextEditingController();
   RegisterController():super(RegisterPage());
   final _http=RegisterHttp();
   
   @override
   void initState(){
       super.initState();
       
   }

   void onRegisterClick() async {
      final phone=textPhoneController.text.trim();
      final pwd=textPwdController.text.trim();
      final repwd=textRePwdController.text.trim();
      final nick=textNickController.text.trim();
      if(phone.isEmpty){
         ToastUtils.show("请输入手机号");
         return;
      }
      if(phone.length!=11){
         ToastUtils.show("手机号格式不正确");
         return;
      }
      if(pwd.length>16||pwd.length<6){

         ToastUtils.show("请填写6～16位密码");
         return;
      }

      if(pwd.isEmpty){
         ToastUtils.show("请输入密码");
         return;
      }
      if(nick.isEmpty){
         ToastUtils.show("请输入昵称");
         return;
      }
      if(pwd!=repwd){
         ToastUtils.show("两次密码输入不一致");
         return;
      }

      showLoading();

      final md5Pwd=md5.convert(const Utf8Encoder().convert(pwd)).toString();
      final result=await _http.httpRegister(phone,md5Pwd,nick);
      dismissLoading();
      if(result.status==200){
         Navigator.of(context).pop();
         ToastUtils.show("注册成功");
      }else{
         ToastUtils.show(result.msg);
      }


   }
   
}
